package constellation.custom;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.Calendar;

public class ClockView extends View {
    Paint paint;
    int hour,minute,seconds;
    Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            getTime();
            invalidate();
            handler.sendEmptyMessageDelayed(1,1000);
        }
    };
    public ClockView(Context context) {
        super(context);
    }

    public ClockView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint();
        getTime();
        handler.sendEmptyMessageDelayed(1,1000);
    }
    public void initPaint(){
        paint=new Paint();
        paint.setStyle(Paint.Style.STROKE);//设置空心
        paint.setAntiAlias(true);//抗锯齿
    }
public  void getTime(){
    Calendar calendar=Calendar.getInstance();
    hour=calendar.get(Calendar.HOUR);
    minute=calendar.get(Calendar.MINUTE);
    seconds=calendar.get(Calendar.SECOND);
}
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setColor(Color.BLACK);

        //绘制表的圆心
        canvas.drawCircle(getWidth()/2,getHeight()/2,10,paint);
        paint.setStrokeWidth(5);//设置线条的粗细
        paint.setStyle(Paint.Style.STROKE);//设置空心
        canvas.drawCircle(getWidth()/2,getHeight()/2,getWidth()/2-10,paint);
        paint.setStrokeWidth(5);//设置线条的粗细
        canvas.drawCircle(getWidth()/2,getHeight()/2,getWidth()/2-20,paint);
        //绘制刻度线
        //通过旋转画布，绘制刻度线
        paint.setColor(Color.RED);
        for (int i = 0; i < 12; i++) {
            //保存旋转之前的状态
            canvas.save();
            canvas.rotate(360/12*(i+1),getWidth()/2,getHeight()/2);
            canvas.drawLine(getWidth()/2,30,getWidth()/2,40,paint);
            paint.setTextSize(50);

            canvas.drawText((i+1)+"",getWidth()/2-20,100,paint);
            //恢复画布
            canvas.restore();
        }
        //绘制时针
        canvas.save();
        canvas.rotate(hour*30+0.5f*minute,getWidth()/2,getHeight()/2);

        paint.setStrokeWidth(10);
        canvas.drawLine(getWidth()/2,getHeight()/2+10,getWidth()/2,getHeight()/2-getHeight()/5,paint);
        canvas.restore();
        //绘制分针
        paint.setColor(Color.BLUE);
        canvas.save();
        paint.setStrokeWidth(7);
        canvas.rotate(6*minute,getWidth()/2,getHeight()/2);
        canvas.drawLine(getWidth()/2,getHeight()/2+10,getWidth()/2,getHeight()/2-getHeight()/4,paint);
        canvas.restore();
        paint.setColor(Color.GREEN);
        canvas.save();
        paint.setStrokeWidth(5);
        canvas.rotate(6*seconds,getWidth()/2,getHeight()/2);
        canvas.drawLine(getWidth()/2,getHeight()/2+10,getWidth()/2,getHeight()/2-getHeight()/3,paint);
        canvas.restore();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //        获取使用自定义view时，在布局当中设定的，宽高的模式
        int wmode = MeasureSpec.getMode(widthMeasureSpec);
        int hmode = MeasureSpec.getMode(heightMeasureSpec);
//        获取到宽高的最大尺寸
        int wsize = MeasureSpec.getSize(widthMeasureSpec);
        int hsize = MeasureSpec.getSize(heightMeasureSpec);
        //画圆形，按照宽度和高度小的那个半径
        switch (wmode) {
            case MeasureSpec.AT_MOST:   //wrap_content
                if (hmode == MeasureSpec.AT_MOST) {
                    wsize = hsize = 300;
                }else {
                    wsize = hsize;
                }
                break;
            case MeasureSpec.EXACTLY: //march_parent   精确值
                wsize = hsize = Math.min(wsize,hsize);
                break;
            //        将计算好的宽高设置给自定义view

        }
        setMeasuredDimension(wsize,hsize);
    }
}
